package com.example.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 数据库连接工具
 */
public class DBUtil {
    // derby 的连接
    public static Connection getConnection() {
        Connection conn = null;
        // 拿到当前项目路径
        String classPath = System.getProperty("user.dir");
        String DBURL = "jdbc:derby:" + classPath + "\\src\\main\\webapp\\WEB-INF\\lib\\mydb";
        String DERBY_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";

        try {
            Class.forName(DERBY_DRIVER);

            try {
                conn = DriverManager.getConnection(DBURL);
                System.out.println("数据库连接成功");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return conn;
    }


    // mysql 的连接
    /*public static Connection getConnection() {

     *//**
     * url:连接的数据库
     * username:用户名
     * password:密码
     *//*
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/panload?";
        String username = "root";
        String password = "123456";
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }*/
}
